<template>
  <el-card class="box-card">
    <div
      slot="header"
      class="clearfix"
    >
      <span>注册</span>
      <el-button
        style="float: right; padding: 3px 0"
        type="text"
        @click="godl"
      >去登录</el-button>
    </div>
    <!--  -->
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
    >
      <el-form-item
        label="姓名"
        prop="name"
      >
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>

      <el-form-item
        label="年龄"
        prop="age"
      >
        <el-input v-model="ruleForm.age"></el-input>
      </el-form-item>

      <el-form-item
        label="性别"
        prop="gender"
      >
        <el-radio-group v-model="ruleForm.gender">
          <el-radio label="男">男</el-radio>
          <el-radio label="女">女</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item
        label="手机号"
        prop="phone"
      >
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>

      <el-form-item
        label="地址"
        prop="adress"
      >
        <el-input v-model="ruleForm.adress"></el-input>
      </el-form-item>

      <el-form-item
        label="密码"
        prop="loginKey"
      >
        <el-input v-model="ruleForm.loginKey"></el-input>
      </el-form-item>

      <el-form-item
        label="用户名"
        prop="loginName"
      >
        <el-input v-model="ruleForm.loginName"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          @click="submitForm('ruleForm')"
        >提交</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>

    </el-form>
    <!--  -->
  </el-card>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapState, mapMutations, mapActions } = createNamespacedHelpers("reg");

export default {
  name: "Reg",
  data() {
    return {
      rules: {
        name: [{ required: true, message: "请输入性别", trigger: "blur" }],
        age: [{ required: true, message: "请输入年龄", trigger: "blur" }],
        gender: [{ required: true, message: "请输入性别", trigger: "blur" }],
        phone: [{ required: true, message: "请输入手机号", trigger: "blur" }],
        adress: [{ required: true, message: "请输入微信", trigger: "blur" }],
        loginKey: [{ required: true, message: "请输入密码", trigger: "blur" }],
        loginName: [{ required: true, message: "请输入用户名", trigger: "blur" }]
      }
    };
  },

  mounted() {},

  methods: {
    ...mapMutations(["setRuleForm"]),
    ...mapActions(["getZc", "getUsername"]),
    reset() {
      this.setRuleForm();
    },
    godl() {
      this.$router.push("/");
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.getUsername(this).then(() => {
            this.getZc()
              .then(() => {
                this.$message({
                  message: "注册成功",
                  type: "success"
                });
              })
              .then(() => {
                this.godl();
              });
          });
        } else {
          console.log("信息不能为空");
          return false;
        }
      });
    }
  },
  computed: {
    ...mapState(["ruleForm"])
  }
};
</script>

<style scoped>
.box-card {
  margin: 0 auto;
}
</style>
